/*
 * Copyright (C) 2020 1km Group Holding Limited
 */

#include "rtc_time.h"
#include "stdio.h"
#include "string.h"

#define RTC1_PORT_NUM 1
/* define dev */
rtc_dev_t rtc1;

static int32_t rtc_time_set_year(uint8_t year)
{
	int32_t ret = 0;
	rtc_time_t time_buf;

	/* get rtc current time */
	ret = hal_rtc_get_time(&rtc1, &time_buf);
	if (ret != 0) {
		printf("rtc1 get time error !\n");
		return -1;
	}

	time_buf.year = year;

	/* set rtc1 time*/
	ret = hal_rtc_set_time(&rtc1, &time_buf);
	if (ret != 0) {
		printf("rtc1 set time error !\n");
		return -1;
	}else{
		return 0;
	}
	
}

static int32_t rtc_time_get_year()
{
	int32_t ret = 0;
	rtc_time_t time_buf;

	/* get rtc current time */
	ret = hal_rtc_get_time(&rtc1, &time_buf);
	if (ret != 0) {
		printf("rtc1 get time error !\n");
		return -1;
	}

	return time_buf.year;
}

static int32_t rtc_time_set_month(uint8_t month)
{
	int32_t ret = 0;
	rtc_time_t time_buf;

	/* get rtc current time */
	ret = hal_rtc_get_time(&rtc1, &time_buf);
	if (ret != 0) {
		printf("rtc1 get time error !\n");
		return -1;
	}

	time_buf.month = month;

	/* set rtc1 time*/
	ret = hal_rtc_set_time(&rtc1, &time_buf);
	if (ret != 0) {
		printf("rtc1 set time error !\n");
		return -1;
	}else{
		return 0;
	}
	
}

static int32_t rtc_time_get_month()
{
	int32_t ret = 0;
	rtc_time_t time_buf;

	/* get rtc current time */
	ret = hal_rtc_get_time(&rtc1, &time_buf);
	if (ret != 0) {
		printf("rtc1 get time error !\n");
		return -1;
	}

	return time_buf.month;
}

static int32_t rtc_time_set_date(uint8_t date)
{
	int32_t ret = 0;
	rtc_time_t time_buf;

	/* get rtc current time */
	ret = hal_rtc_get_time(&rtc1, &time_buf);
	if (ret != 0) {
		printf("rtc1 get time error !\n");
		return -1;
	}

	time_buf.date = date;

	/* set rtc1 time*/
	ret = hal_rtc_set_time(&rtc1, &time_buf);
	if (ret != 0) {
		printf("rtc1 set time error !\n");
		return -1;
	}else{
		return 0;
	}
	
}

static int32_t rtc_time_get_date()
{
	int32_t ret = 0;
	rtc_time_t time_buf;

	/* get rtc current time */
	ret = hal_rtc_get_time(&rtc1, &time_buf);
	if (ret != 0) {
		printf("rtc1 get time error !\n");
		return -1;
	}

	return time_buf.date;
}

static int32_t rtc_time_set_weekday(uint8_t weekday)
{
	int32_t ret = 0;
	rtc_time_t time_buf;

	/* get rtc current time */
	ret = hal_rtc_get_time(&rtc1, &time_buf);
	if (ret != 0) {
		printf("rtc1 get time error !\n");
		return -1;
	}

	time_buf.weekday = weekday;

	/* set rtc1 time*/
	ret = hal_rtc_set_time(&rtc1, &time_buf);
	if (ret != 0) {
		printf("rtc1 set time error !\n");
		return -1;
	}else{
		return 0;
	}
	
}

static int32_t rtc_time_get_weekday()
{
	int32_t ret = 0;
	rtc_time_t time_buf;

	/* get rtc current time */
	ret = hal_rtc_get_time(&rtc1, &time_buf);
	if (ret != 0) {
		printf("rtc1 get time error !\n");
		return -1;
	}

	return time_buf.weekday;
}

static int32_t rtc_time_set_hr(uint8_t hr)
{
	int32_t ret = 0;
	rtc_time_t time_buf;

	/* get rtc current time */
	ret = hal_rtc_get_time(&rtc1, &time_buf);
	if (ret != 0) {
		printf("rtc1 get time error !\n");
		return -1;
	}

	time_buf.hr = hr;

	/* set rtc1 time*/
	ret = hal_rtc_set_time(&rtc1, &time_buf);
	if (ret != 0) {
		printf("rtc1 set time error !\n");
		return -1;
	}else{
		return 0;
	}
	
}

static int32_t rtc_time_get_hr()
{
	int32_t ret = 0;
	rtc_time_t time_buf;

	/* get rtc current time */
	ret = hal_rtc_get_time(&rtc1, &time_buf);
	if (ret != 0) {
		printf("rtc1 get time error !\n");
		return -1;
	}

	return time_buf.hr;
}

static int32_t rtc_time_set_min(uint8_t min)
{
	int32_t ret = 0;
	rtc_time_t time_buf;

	/* get rtc current time */
	ret = hal_rtc_get_time(&rtc1, &time_buf);
	if (ret != 0) {
		printf("rtc1 get time error !\n");
		return -1;
	}

	time_buf.min = min;

	/* set rtc1 time*/
	ret = hal_rtc_set_time(&rtc1, &time_buf);
	if (ret != 0) {
		printf("rtc1 set time error !\n");
		return -1;
	}else{
		return 0;
	}
	
}

static int32_t rtc_time_get_min()
{
	int32_t ret = 0;
	rtc_time_t time_buf;

	/* get rtc current time */
	ret = hal_rtc_get_time(&rtc1, &time_buf);
	if (ret != 0) {
		printf("rtc1 get time error !\n");
		return -1;
	}

	return time_buf.min;
}

static int32_t rtc_time_set_sec(uint8_t sec)
{
	int32_t ret = 0;
	rtc_time_t time_buf;

	/* get rtc current time */
	ret = hal_rtc_get_time(&rtc1, &time_buf);
	if (ret != 0) {
		printf("rtc1 get time error !\n");
		return -1;
	}

	time_buf.sec = sec;

	/* set rtc1 time*/
	ret = hal_rtc_set_time(&rtc1, &time_buf);
	if (ret != 0) {
		printf("rtc1 set time error !\n");
		return -1;
	}else{
		return 0;
	}
	
}

static int32_t rtc_time_get_sec()
{
	int32_t ret = 0;
	rtc_time_t time_buf;

	/* get rtc current time */
	ret = hal_rtc_get_time(&rtc1, &time_buf);
	if (ret != 0) {
		printf("rtc1 get time error !\n");
		return -1;
	}

	return time_buf.sec;
}

int32_t xp_rtc_time_finalize()
{
	int32_t ret = 0;
	/* finalize rtc1 */
	ret = hal_rtc_finalize(&rtc1);
	if(0 != ret){
		return -1;
	}else{
		return 0;
	}
}

int32_t xp_rtc_time_init()
{
	int32_t ret = 0;
	rtc_config_t rtc_cfg;
	rtc_time_t	 time_buf;

	/* rtc port set */
	rtc1.port = RTC1_PORT_NUM;

	/* set to DEC format */
	rtc1.config.format = HAL_RTC_FORMAT_DEC;

	/* init rtc1 with the given settings */
	ret = hal_rtc_init(&rtc1);
	if (ret != 0) {
		printf("rtc1 init error !\n");
		return -1;
	}else{
		return 0;
	}

}

char *xp_rtc_get_string(void)
{
	static char Buf[20];
    sprintf(Buf, "20%2d/%2d/%2d %2d:%2d:%2d\n", 	rtc_time_get_year(), \
													rtc_time_get_month(), \
													rtc_time_get_date(), \
													rtc_time_get_hr(), \
													rtc_time_get_min(), \
													rtc_time_get_sec());
    return Buf;
}

int xp_rtc_time_debug(char *type,char *fun,char *param)
{
	int ret = 0;
	u8 val;
	
	if(strcmp(type,"rtc")!=0)return 0;
	if(strcmp(fun,"set_year")==0){
		val = strtoul(param, NULL, 10);
		rtc_time_set_year(val);
		printf("rtc_time_set_year is %d \n", val);
		return 1;
	}else if(strcmp(fun,"get_year")==0){
		val = rtc_time_get_year();
		printf("rtc_time_get_year is %d \n", val);
		return 1;
	}else if(strcmp(fun,"set_month")==0){
		val = strtoul(param, NULL, 10);
		rtc_time_set_month(val);
		printf("rtc_time_set_month is %d \n", val);
		return 1;
	}else if(strcmp(fun,"get_month")==0){
		val = rtc_time_get_month();
		printf("rtc_time_get_month is %d \n", val);
		return 1;
	}else if(strcmp(fun,"set_date")==0){
		val = strtoul(param, NULL, 10);
		rtc_time_set_date(val);
		printf("rtc_time_set_date is %d \n", val);
		return 1;
	}else if(strcmp(fun,"get_date")==0){
		val = rtc_time_get_date();
		printf("rtc_time_get_date is %d \n", val);
		return 1;
	}else if(strcmp(fun,"set_weekday")==0){
		val = strtoul(param, NULL, 10);
		rtc_time_set_weekday(val);
		printf("rtc_time_set_weekday is %d \n", val);
		return 1;
	}else if(strcmp(fun,"get_weekday")==0){
		val = rtc_time_get_weekday();
		printf("rtc_time_get_weekday is %d \n", val);
		return 1;
	}else if(strcmp(fun,"set_hr")==0){
		val = strtoul(param, NULL, 10);
		rtc_time_set_hr(val);
		printf("rtc_time_set_hr is %d \n", val);
		return 1;
	}else if(strcmp(fun,"get_hr")==0){
		val = rtc_time_get_hr();
		printf("rtc_time_get_hr is %d \n", val);
		return 1;
	}else if(strcmp(fun,"set_min")==0){
		val = strtoul(param, NULL, 10);
		rtc_time_set_min(val);
		printf("rtc_time_set_min is %d \n", val);
		return 1;
	}else if(strcmp(fun,"get_min")==0){
		val = rtc_time_get_min();
		printf("rtc_time_get_min is %d \n", val);
		return 1;
	}else if(strcmp(fun,"set_sec")==0){
		val = strtoul(param, NULL, 10);
		rtc_time_set_sec(val);
		printf("rtc_time_set_sec is %d \n", val);
		return 1;
	}else if(strcmp(fun,"get_sec")==0){
		val = rtc_time_get_sec();
		printf("rtc_time_get_sec is %d \n", val);
		return 1;
	}else if(strcmp(fun,"get_time")==0){
		printf("20%2d/%2d/%2d %2d:%2d:%2d\n", 	rtc_time_get_year(), \
												rtc_time_get_month(), \
												rtc_time_get_date(), \
												rtc_time_get_hr(), \
												rtc_time_get_min(), \
												rtc_time_get_sec());
		return 1;
	}
	else{
			return 0;
	}
	return 1;

}


